package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.domain.vo.ShopEchart;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.java2d.pipe.AAShapePipe;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-08-05
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {

        //1、空参数校验
        if(StringUtils.isEmpty(shop.getName())){
            throw new CustomException("店铺名不能为空");
        }

        //使用百度AI校验店铺名称是否合法
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("店铺名称不合法");
        }

        //2、校验店铺是否已经入驻过了
        Shop shop1 = shopMapper.queryByName(shop.getName());
        //2.1 如果店铺已经入驻过了 就抛出异常
        if(shop1 != null){
            throw new CustomException("店铺已经入驻过!");
        }

        //3、初始化参数
        shop.setState(0);//设置店铺为待审核状态
        shop.setRegisterTime(new Date());//设置注册时间

        //4、查询该店长的账是否已经存在
        Employee admin = shop.getAdmin();
        Employee emp = employeeMapper.queryByUserName(admin.getUsername());

        //保存店铺信息
        shopMapper.add(shop);

        //4.1 如果存在 检查员工的shop_id 是否为null
        if(emp != null && emp.getShop_id() != null){
            throw new CustomException("该员工不合法，请确认后重试!");
        }else if(emp != null){
            emp.setShop_id(shop.getId());
            employeeMapper.update(emp);
        }
        admin.setState(0);

        //保存员工
        employeeMapper.add(admin);
        //更新店铺的店长id
        shop.setAdminId(admin.getId());
        //更新店铺
        shopMapper.update(shop);
        //给员工绑定店铺id
        admin.setShop_id(shop.getId());
        //更新员工
        employeeMapper.update(admin);

    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog log) {
        log.setState(0);//审核日子表示审核通过
        log.setAuditId(334L);//设置审核人
        log.setAuditTime(new Date());//设置审核时间
        shopAuditLogMapper.add(log);//保存审核日志

        Shop shop = shopMapper.loadOne(log.getShopId());//查询对应的店铺
        shop.setState(1);//修改店铺状态
        shopMapper.update(shop);//更新店铺信息
        System.out.println(shop);

        //获取店长信息 这一步是为了获取店长的邮箱，审核通过后给店长发邮件
        Employee employee = employeeMapper.loadOne(shop.getAdminId());


        //发送邮件
        SimpleMailMessage sm = new SimpleMailMessage();
        sm.setFrom("1782169885@qq.com");//邮件来自哪里
        sm.setTo(employee.getEmail());//邮件发送到哪里
        sm.setSubject("审核信息");
        sm.setText("恭喜你审核通过");
        javaMailSender.send(sm);

    }

    @Override
    @Transactional
    public void reject(ShopAuditLog log) {
        log.setState(-1);//审核日子表示审核通过
        log.setAuditId(334L);//设置审核人
        log.setAuditTime(new Date());//设置审核时间
        shopAuditLogMapper.add(log);//保存审核日志

        Shop shop = shopMapper.loadOne(log.getShopId());//查询对应的店铺
        shop.setState(-1);//修改店铺状态
        shopMapper.update(shop);//更新店铺信息
        System.out.println(shop);

        //获取店长信息 这一步是为了获取店长的邮箱，审核通过后给店长发邮件
        Employee employee = employeeMapper.loadOne(shop.getAdminId());


        //发送邮件
        SimpleMailMessage sm = new SimpleMailMessage();
        sm.setFrom("1782169885@qq.com");//邮件来自哪里
        sm.setTo(employee.getEmail());//邮件发送到哪里
        sm.setSubject("审核信息");
        sm.setText("<h1>审核失败</h1><br>"+
                "<span>失败原因："+log.getNote()+"</span><br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>");
        javaMailSender.send(sm);
    }

    @Override
    public Map<String,Object> shopEchart() {
        List<ShopEchart> se = shopMapper.shopEchart();
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        for (ShopEchart a : se) {
            if(a.getState().equals("-1")){
                a.setState("未通过");
            }else if (a.getState().equals("0")){
                a.setState("待审核");
            }else {
                a.setState("驳回");
            }
            x.add(a.getState());
            y.add(a.getCount());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return map;
    }
}
